package com.zwy.ubts.utils;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

/**
 * @Author
 * @Description:
 * @Date Created in 21:07 2023/1/3
 */
public class Mail {

    private static void sendMail(String title, String content, String toEmailAddres) throws Exception {

        System.out.println("title:["+title+"]   toEmailAddres:["+toEmailAddres+"]");
        String host = PropertiesFile.getKey("mail.host");        //邮箱服务器地址
        String port = "25";                        //发送邮件的端口
        String auth = "true";                     //是否需要进行身份验证,视调用的邮箱而定，比方说QQ邮箱就需要，否则就会发送失败
        String protocol = "smtp";                  //传输协议
        String mailFrom = PropertiesFile.getKey("mail.username");   //发件人邮箱
        String personalName = PropertiesFile.getKey("mail.likename");         //发件人邮箱别名
        String username = PropertiesFile.getKey("mail.username");   //发件人邮箱用户名
        String password = PropertiesFile.getKey("mail.password");               //发件人邮箱密码
        String mailDebug = "false";                //是否开启debug
        String contentType = null;                 //邮件正文类型

        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", auth == null ? "true" : auth);
        props.put("mail.transport.protocol", protocol == null ? "smtp" : protocol);
        props.put("mail.smtp.port", port == null ? "25" : port);
        props.put("mail.debug", mailDebug == null ? "false" : mailDebug);
        Session mailSession = Session.getInstance(props);


        MimeMessage message = new MimeMessage(mailSession);// 设置session,和邮件服务器进行通讯。
        message.setSubject(title); // 设置邮件主题
        message.setContent(content, contentType == null ? "text/html;charset=UTF-8" : contentType);// 设置邮件正文
        // 设置邮件发送日期
        message.setSentDate(new Date());
        InternetAddress address = new InternetAddress(mailFrom, personalName);
        // 设置邮件发送者的地址
        message.setFrom(address);
        // 设置邮件接收方的地址
        message.setRecipients(Message.RecipientType.TO, toEmailAddres);
        Transport transport = null;
        transport = mailSession.getTransport();
        message.saveChanges();

        transport.connect(host, username, password);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    public static void sendMailCode(String toMail,String user,String code){
        String time = PropertiesFile.getKey("mailcode.time");
        String titleRoot = PropertiesFile.getKey("mailcode.title");
        String opUser = PropertiesFile.getKey("mailcode.opuser");
        String title ="【"+titleRoot+"】";
        String titlecode ="【"+titleRoot+"-验证码】";

        String body = "<div style=\"background-color:#ECECEC; padding: 35px;\">\n" +
                "    <table cellpadding=\"0\" align=\"center\"\n" +
                "           style=\"width: 800px;height: 100%; margin: 0px auto; text-align: left; position: relative; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; font-size: 14px; font-family:微软雅黑, 黑体; line-height: 1.5; box-shadow: rgb(153, 153, 153) 0px 0px 5px; border-collapse: collapse; background-position: initial initial; background-repeat: initial initial;background:#fff;\">\n" +
                "        <tbody>\n" +
                "        <tr>\n" +
                "            <th valign=\"middle\"\n" +
                "                style=\"height: 25px; line-height: 25px; padding: 15px 35px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: RGB(148,0,211); background-color: RGB(148,0,211); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px;\">\n" +
                "                <font face=\"微软雅黑\" size=\"5\" style=\"color: rgb(255, 255, 255); \">"+titleRoot+"</font>\n" +
                "            </th>\n" +
                "        </tr>\n" +
                "        <tr>\n" +
                "            <td style=\"word-break:break-all\">\n" +
                "                <div style=\"padding:25px 35px 40px; background-color:#fff;opacity:0.8;\">\n" +
                "\n" +
                "                    <h2 style=\"margin: 5px 0px; \">\n" +
                "                        <font color=\"#333333\" style=\"line-height: 20px; \">\n" +
                "                            <font style=\"line-height: 22px; \" size=\"4\">\n" +
                "                                尊敬的用户："+user+"</font>\n" +
                "                        </font>\n" +
                "                    </h2>\n" +
                "                    <!-- 中文 -->\n" +
                "                    <p>您好！感谢您使用"+title+"，您的账号正在进行邮箱验证，验证码为：<font color=\"#ff8c00\">["+code+"]</font>，有效期"+time+"分钟，请尽快填写验证码完成验证！</p><br>\n" +
                "                  \n" +
                "                    <div style=\"width:100%;margin:0 auto;\">\n" +
                "                        <div style=\"padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;text-align:right\">\n" +
                "                            <p>──"+opUser+"</p>\n" +
                "                            <br>\n" +
                "                            <p>此为系统邮件，请勿回复<br>\n" +
                "                                Please do not reply to this system email\n" +
                "                            </p>\n" +
                "                            <!--<p>©***</p>-->\n" +
                "                        </div>\n" +
                "                    </div>\n" +
                "                </div>\n" +
                "            </td>\n" +
                "        </tr>\n" +
                "        </tbody>\n" +
                "    </table>\n" +
                "</div>";


        try {
            sendMail(titlecode,body,toMail);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void sendMailBookSell(String toMail,String user,String bookName){
        String time = PropertiesFile.getKey("mailcode.time");
        String titleRoot = PropertiesFile.getKey("mailcode.title");
        String opUser = PropertiesFile.getKey("mailcode.opuser");
        String title ="【"+titleRoot+"】";
        String titlecode ="【"+titleRoot+"-售卖提示】";

        //String body = "<div>user："+user+" 你的书 《"+bookName+"》已经卖出，请尽快发货</div>";
        String body = "<div style=\"background-color:#ECECEC; padding: 35px;\">\n" +
                "    <table cellpadding=\"0\" align=\"center\"\n" +
                "           style=\"width: 800px;height: 100%; margin: 0px auto; text-align: left; position: relative; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; font-size: 14px; font-family:微软雅黑, 黑体; line-height: 1.5; box-shadow: rgb(153, 153, 153) 0px 0px 5px; border-collapse: collapse; background-position: initial initial; background-repeat: initial initial;background:#fff;\">\n" +
                "        <tbody>\n" +
                "        <tr>\n" +
                "            <th valign=\"middle\"\n" +
                "                style=\"height: 25px; line-height: 25px; padding: 15px 35px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: RGB(148,0,211); background-color: RGB(148,0,211); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px;\">\n" +
                "                <font face=\"微软雅黑\" size=\"5\" style=\"color: rgb(255, 255, 255); \">"+titleRoot+"</font>\n" +
                "            </th>\n" +
                "        </tr>\n" +
                "        <tr>\n" +
                "            <td style=\"word-break:break-all\">\n" +
                "                <div style=\"padding:25px 35px 40px; background-color:#fff;opacity:0.8;\">\n" +
                "\n" +
                //下面是主题内容 可以改变
                "                    <h2 style=\"margin: 5px 0px; \">\n" +
                "                        <font color=\"#333333\" style=\"line-height: 20px; \">\n" +
                "                            <font style=\"line-height: 22px; \" size=\"4\">\n" +
                "                                尊敬的用户："+user+"</font>\n" +
                "                        </font>\n" +
                "                    </h2>\n" +
                "                    <!-- 中文 -->\n" +
                "                    <p>您好！感谢您使用"+title+"，你的书 《"+bookName+"》已经卖出，请尽快发货。</p><br>\n" +
                "                  \n" +
                //主体内容结束
                "                    <div style=\"width:100%;margin:0 auto;\">\n" +
                "                        <div style=\"padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;text-align:right\">\n" +
                "                            <p>──"+opUser+"</p>\n" +
                "                            <br>\n" +
                "                            <p>此为系统邮件，请勿回复<br>\n" +
                "                                Please do not reply to this system email\n" +
                "                            </p>\n" +
                "                            <!--<p>©***</p>-->\n" +
                "                        </div>\n" +
                "                    </div>\n" +
                "                </div>\n" +
                "            </td>\n" +
                "        </tr>\n" +
                "        </tbody>\n" +
                "    </table>\n" +
                "</div>";

        try {
            sendMail(titlecode,body,toMail);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void sendMailBookBuy(String toMail,String user,String bookName,Integer orderId,String kdId){
        String time = PropertiesFile.getKey("mailcode.time");
        String titleRoot = PropertiesFile.getKey("mailcode.title");
        String opUser = PropertiesFile.getKey("mailcode.opuser");
        String title ="【"+titleRoot+"】";
        String titlecode ="【"+titleRoot+"-发货提示】";

        //String body = "<div>user："+user+" 你的书 《"+bookName+"》已经卖出，请尽快发货</div>";
        String body = "<div style=\"background-color:#ECECEC; padding: 35px;\">\n" +
                "    <table cellpadding=\"0\" align=\"center\"\n" +
                "           style=\"width: 800px;height: 100%; margin: 0px auto; text-align: left; position: relative; border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 5px; border-bottom-left-radius: 5px; font-size: 14px; font-family:微软雅黑, 黑体; line-height: 1.5; box-shadow: rgb(153, 153, 153) 0px 0px 5px; border-collapse: collapse; background-position: initial initial; background-repeat: initial initial;background:#fff;\">\n" +
                "        <tbody>\n" +
                "        <tr>\n" +
                "            <th valign=\"middle\"\n" +
                "                style=\"height: 25px; line-height: 25px; padding: 15px 35px; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: RGB(148,0,211); background-color: RGB(148,0,211); border-top-left-radius: 5px; border-top-right-radius: 5px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px;\">\n" +
                "                <font face=\"微软雅黑\" size=\"5\" style=\"color: rgb(255, 255, 255); \">"+titleRoot+"</font>\n" +
                "            </th>\n" +
                "        </tr>\n" +
                "        <tr>\n" +
                "            <td style=\"word-break:break-all\">\n" +
                "                <div style=\"padding:25px 35px 40px; background-color:#fff;opacity:0.8;\">\n" +
                "\n" +
                //下面是主题内容 可以改变
                "                    <h2 style=\"margin: 5px 0px; \">\n" +
                "                        <font color=\"#333333\" style=\"line-height: 20px; \">\n" +
                "                            <font style=\"line-height: 22px; \" size=\"4\">\n" +
                "                                尊敬的用户："+user+"</font>\n" +
                "                        </font>\n" +
                "                    </h2>\n" +
                "                    <!-- 中文 -->\n" +
                "                    <p>您好！感谢您使用"+title+"，你购买的书 《"+bookName+"》已经发货，订单号为："+orderId+"，运单号为<font color=\"#ff8c00\">["+kdId+"]</font>，请确认物流信息。</p><br>\n" +
                "                  \n" +
                //主体内容结束
                "                    <div style=\"width:100%;margin:0 auto;\">\n" +
                "                        <div style=\"padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;text-align:right\">\n" +
                "                            <p>──"+opUser+"</p>\n" +
                "                            <br>\n" +
                "                            <p>此为系统邮件，请勿回复<br>\n" +
                "                                Please do not reply to this system email\n" +
                "                            </p>\n" +
                "                            <!--<p>©***</p>-->\n" +
                "                        </div>\n" +
                "                    </div>\n" +
                "                </div>\n" +
                "            </td>\n" +
                "        </tr>\n" +
                "        </tbody>\n" +
                "    </table>\n" +
                "</div>";

        try {
            sendMail(titlecode,body,toMail);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    //public static void main(String[] args) {
    //    sendMailCode("1465865337@qq.com","周文英","love yg");
    //}

}
